package com.example.cd5160866.urlapp.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;

import com.example.cd5160866.urlapp.R;


public class ResizeDrawableTextView extends TextView {

    private int startDrawableWidth;
    private int startDrawableHeight;
    private int endDrawableWidth;
    private int endDrawableHeight;

    private int defaultDrawableHeight;

    public ResizeDrawableTextView(Context context) {
        this(context, null);
    }

    public ResizeDrawableTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextDrawable);
        startDrawableWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_startDrawableWidth, 0);
        startDrawableHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_startDrawableHeight, 0);
        endDrawableWidth = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_endDrawableWidth, 0);
        endDrawableHeight = typedArray.getDimensionPixelOffset(R.styleable.TextDrawable_endDrawableHeight, 0);
        typedArray.recycle();
        updateDefaultHeight();
    }

    /**
     * 获得当前字体下文字高度，为后续的drawable设置默认大小
     */
    private void updateDefaultHeight() {
        Paint paint = new Paint();
        Rect rect = new Rect();
        String textStr = getText().toString();
        float textSize = getTextSize();
        paint.setTextSize(textSize);
        paint.getTextBounds(textStr, 0, textStr.length(), rect);
        defaultDrawableHeight = rect.height();
    }

    //是否需要重新绘
    private boolean needResizeDrawable = true;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //DrawableStart
        Drawable drawableStart = getCompoundDrawables()[0];
        if (drawableStart != null) {
            //是否需要重新计算Drawable大小
            if ((startDrawableWidth == 0 || startDrawableHeight == 0) &&
                    drawableStart.getIntrinsicHeight() != defaultDrawableHeight) {
                startDrawableWidth = (int) ((float) (defaultDrawableHeight * drawableStart.getIntrinsicWidth()) / (float) drawableStart.getIntrinsicHeight());
                startDrawableHeight = defaultDrawableHeight;
            }
            drawableStart.setBounds(0, 0, startDrawableWidth, startDrawableHeight);
        }

        //DrawableEnd
        Drawable drawableEnd = getCompoundDrawables()[2];
        if (drawableEnd != null) {
            if ((endDrawableWidth == 0 || endDrawableHeight == 0) &&
                    drawableEnd.getIntrinsicHeight() != defaultDrawableHeight) {
                endDrawableWidth = (int) ((float) (defaultDrawableHeight * drawableEnd.getIntrinsicWidth()) / (float) drawableEnd.getIntrinsicHeight());
                endDrawableHeight = defaultDrawableHeight;
            }
            drawableEnd.setBounds(0, 0, endDrawableWidth, endDrawableHeight);
        }

        //draw
        if (needResizeDrawable) {
            setCompoundDrawables(drawableStart, getCompoundDrawables()[1], drawableEnd, getCompoundDrawables()[3]);
            this.needResizeDrawable = false;
        }
    }

    public void setDrawableStartSize(int width, int height) {
        this.startDrawableWidth = width;
        this.startDrawableHeight = height;
        updateDefaultHeight();
        postInvalidate();
    }

    public void setDrawableEndSize(int width, int height) {
        this.endDrawableWidth = width;
        this.endDrawableHeight = height;
        updateDefaultHeight();
        postInvalidate();
    }
}
